<template>
  <view class="container">
    <!-- 搜索栏 -->
    <view class="search-section">
      <view class="search-box">
        <text class="iconfont icon-search"></text>
        <input 
          type="text" 
          v-model="keyword" 
          placeholder="搜索收藏商品"
          @confirm="handleSearch"
        />
      </view>
    </view>
    
    <!-- 分类筛选 -->
    <view class="filter-section">
      <scroll-view scroll-x class="filter-scroll">
        <view 
          class="filter-item" 
          v-for="(item, index) in categoryList" 
          :key="index"
          :class="{ active: currentCategory === item.id }"
          @tap="selectCategory(item.id)"
        >
          <text>{{item.name}}</text>
        </view>
      </scroll-view>
    </view>
    
    <!-- 商品列表 -->
    <view class="product-list">
      <view 
        class="product-item" 
        v-for="(item, index) in productList" 
        :key="index"
      >
        <image :src="item.image" mode="aspectFill" @tap="goDetail(item)"></image>
        <view class="product-info">
          <text class="title" @tap="goDetail(item)">{{item.title}}</text>
          <view class="price-wrap">
            <text class="price">¥{{item.price}}</text>
            <text class="original-price" v-if="item.originalPrice">¥{{item.originalPrice}}</text>
          </view>
          <view class="action">
            <button class="buy-btn" @tap="goDetail(item)">立即购买</button>
            <text class="iconfont icon-delete" @tap="cancelFavorite(item)"></text>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 空状态 -->
    <view class="empty" v-if="productList.length === 0">
      <image src="/static/images/empty-favorite.png" mode="aspectFit"></image>
      <text>暂无收藏商品</text>
      <button class="go-shopping" @tap="goShopping">去逛逛</button>
    </view>
    
    <!-- 加载更多 -->
    <view class="loading-more" v-if="productList.length > 0">
      <text v-if="loading">加载中...</text>
      <text v-else-if="hasMore" @tap="loadMore">加载更多</text>
      <text v-else>没有更多了</text>
    </view>
  </view>
</template>

<script>
import { favoriteApi, productApi } from '@/api/index'
import { showToast, showModal } from '@/utils/common'

export default {
  data() {
    return {
      keyword: '',
      categoryList: [],
      currentCategory: '',
      productList: [],
      page: 1,
      pageSize: 10,
      loading: false,
      hasMore: true
    }
  },
  onLoad() {
    this.getCategoryList()
    this.getProductList()
  },
  methods: {
    // 获取分类列表
    async getCategoryList() {
      try {
        const res = await productApi.getCategoryList()
        this.categoryList = [{ id: '', name: '全部' }, ...res]
      } catch (error) {
        showToast('分类列表加载失败')
      }
    },
    
    // 选择分类
    selectCategory(id) {
      this.currentCategory = id
      this.page = 1
      this.hasMore = true
      this.getProductList()
    },
    
    // 处理搜索
    handleSearch() {
      this.page = 1
      this.hasMore = true
      this.getProductList()
    },
    
    // 获取商品列表
    async getProductList() {
      if (this.loading || !this.hasMore) return
      
      this.loading = true
      try {
        const res = await favoriteApi.getFavoriteList({
          page: this.page,
          pageSize: this.pageSize,
          keyword: this.keyword,
          categoryId: this.currentCategory
        })
        
        if (this.page === 1) {
          this.productList = res.list
        } else {
          this.productList = [...this.productList, ...res.list]
        }
        
        this.hasMore = res.list.length === this.pageSize
        this.page++
      } catch (error) {
        showToast('商品列表加载失败')
      } finally {
        this.loading = false
      }
    },
    
    // 加载更多
    loadMore() {
      this.getProductList()
    },
    
    // 取消收藏
    async cancelFavorite(product) {
      try {
        await showModal('提示', '确定要取消收藏该商品吗？')
        await favoriteApi.cancelFavorite(product.id)
        showToast('取消收藏成功')
        this.productList = this.productList.filter(item => item.id !== product.id)
      } catch (error) {
        if (error) {
          showToast('取消收藏失败')
        }
      }
    },
    
    // 去商品详情
    goDetail(product) {
      uni.navigateTo({
        url: `/pages/product/detail?id=${product.id}`
      })
    },
    
    // 去购物
    goShopping() {
      uni.switchTab({
        url: '/pages/index/index'
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
}

.search-section {
  background: #fff;
  padding: 20rpx 30rpx;
  
  .search-box {
    display: flex;
    align-items: center;
    background: #f5f5f5;
    border-radius: 30rpx;
    padding: 10rpx 20rpx;
    
    .icon-search {
      font-size: 32rpx;
      color: #999;
      margin-right: 10rpx;
    }
    
    input {
      flex: 1;
      font-size: 28rpx;
      color: #333;
    }
  }
}

.filter-section {
  background: #fff;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f5f5f5;
  
  .filter-scroll {
    white-space: nowrap;
    padding: 0 20rpx;
    
    .filter-item {
      display: inline-block;
      padding: 10rpx 30rpx;
      margin-right: 20rpx;
      border-radius: 30rpx;
      background: #f5f5f5;
      
      &:last-child {
        margin-right: 0;
      }
      
      text {
        font-size: 24rpx;
        color: #666;
      }
      
      &.active {
        background: #ff0000;
        
        text {
          color: #fff;
        }
      }
    }
  }
}

.product-list {
  padding: 20rpx;
  
  .product-item {
    display: flex;
    background: #fff;
    border-radius: 12rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;
    
    image {
      width: 200rpx;
      height: 200rpx;
      border-radius: 8rpx;
      margin-right: 20rpx;
    }
    
    .product-info {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      
      .title {
        font-size: 28rpx;
        color: #333;
        margin-bottom: 10rpx;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
      }
      
      .price-wrap {
        margin-bottom: 20rpx;
        
        .price {
          font-size: 32rpx;
          color: #ff0000;
          font-weight: bold;
          margin-right: 10rpx;
        }
        
        .original-price {
          font-size: 24rpx;
          color: #999;
          text-decoration: line-through;
        }
      }
      
      .action {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .buy-btn {
          width: 160rpx;
          height: 60rpx;
          line-height: 60rpx;
          text-align: center;
          background: #ff0000;
          color: #fff;
          border-radius: 30rpx;
          font-size: 24rpx;
        }
        
        .icon-delete {
          font-size: 40rpx;
          color: #999;
        }
      }
    }
  }
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 200rpx;
  
  image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
  }
  
  text {
    font-size: 28rpx;
    color: #999;
    margin-bottom: 30rpx;
  }
  
  .go-shopping {
    width: 200rpx;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    background: #ff0000;
    color: #fff;
    border-radius: 40rpx;
    font-size: 28rpx;
  }
}

.loading-more {
  text-align: center;
  padding: 30rpx 0;
  
  text {
    font-size: 24rpx;
    color: #999;
  }
}
</style> 